<template>
    <el-card class="box-card" style="margin: 20px">
      <div>
        <el-form :inline="true" :model="dictTypeDTO" class="demo-form-inline">
          <el-form-item>
            <el-input
              v-model="dictTypeDTO.name"
              clearable
              placeholder="角色名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="openDialog()">新增</el-button>
          </el-form-item>
        </el-form>
        <el-table ref="multipleTable" :data="dataList" tooltip-effect="dark" style="width: 100%" row-key="type"
        >
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="id"
            label="字典编号"
            width="120">
          </el-table-column>
          <el-table-column
            prop="name"
            label="字典名称"
            width="120">
          </el-table-column>
          <el-table-column
            prop="type"
            label="字典类型"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="status"
            label="状态"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column fixed="right" label="操作"  >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(scope.$index,scope.row)">修改</el-button>
              <el-button
                type="danger"
                size="small"
                @click="handleDelete(scope.row.id)">删除</el-button>
              <el-button
                type="info"
                size="small"
                @click="show(scope.row.type)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="dictTypeDTO.pageIndex"
          :page-sizes="[5, 10, 20, 50, 100]"
          :page-size="dictTypeDTO.pageSize"
          :total="total"
          layout=" -> ,total,sizes, prev, pager, next, jumper"
          style="margin-top:20px;"
        >
        </el-pagination>
      </div>
      <el-dialog
        :title="dataDialogFrom.id===0?'新增用户':'修改用户'"

        width="35%" :visible.sync="dialogFormVisible" @close="closeDialog()" >
        <el-form :model="dataDialogFrom"
                 :rules="rules"
                 ref="ruleFrom">
          <el-form-item label="字典名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="dataDialogFrom.name" placeholder="字典名称"
                      style="width: 300px;"></el-input>
          </el-form-item>
          <el-form-item label="字典类型" :label-width="formLabelWidth" prop="type">
            <el-input v-model="dataDialogFrom.type" placeholder="字典类型"
                      style="width: 300px;"></el-input>
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
            <el-input v-model="dataDialogFrom.status" placeholder="状态"
                      style="width: 300px;"></el-input>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
            <el-input v-model="dataDialogFrom.remark"  placeholder="备注"
                      style="width: 300px;"></el-input>
          </el-form-item>

        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSubmitFromData('ruleFrom')">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
</template>

<script>

import dectApi from "@/utils/dict"

export default {
  name: "index",
  data(){
    return {
      rules:{
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
        ],
        orderNum: [
          {  required: true, message: '请输入排序编号', trigger: 'blur'}
        ],
      },
      bb:false,
      formLabelWidth:"120px",
      totalPage:0,
      dict:[],
      detailList:[],
      dictIds:[],
      dataList:[],
      dataListLoading:false,
      dialogFormVisible:false,
      dialogFormSubmitVisible:false,
      dictTypeDTO:{
        pageIndex:1,
        pageSize:5,
        name:"",
      },
      dataDialogFrom:{
        id:0,
      },
      total: 0
    }
  },
  methods:{
    show(type){
      this.$router.push({ path: '/operation/dict', query: { type: type } })
    },
    sizeChangeHandle(val){
      this.dictTypeDTO.pageSize=val
      this.getDataList()
    },
    currentChangeHandle(val){
      this.dictTypeDTO.pageIndex=val
      this.getDataList()
    },
    getDataList(){
      this.dataListLoading=true
      dectApi.getList(this.dictTypeDTO).then(res=>{
        console.log(res.list)
        this.dataList=res.list
        this.total=res.total
        this.dataListLoading=false
      })
    },
    closeDialog(){
      this.dataDialogFrom={
        id: 0,
      }
    },
    openDialog(){
        //打开需要更新的用户信息窗口
        this.dialogFormVisible=true
    },
    handleEdit(index,row){
      this.dialogFormVisible=true
      dectApi.getById(row.id).then(res=>{
        //所有的角色信息
        this.dictIds=row.id
        this.dataDialogFrom=res
      })
    },
    handleSubmitFromData(fromName){
      this.$refs[fromName].validate((valid) => {
        if (valid) {
          this.dialogFormSubmitVisible=true
          this.dataDialogFrom.dictIds=this.dictIds
          dectApi.save(this.dataDialogFrom).then((res)=>{
            this.dialogFormVisible=false //关闭窗口
            //清空添加的数据表单
            this.dataDialogFrom={
              userId:0,
            }
            this.dialogFormSubmitVisible=false
            //刷新数据
            this.getDataList()
          })
        } else {
          return false;
        }
      });
    },
    handleDelete(id){
      alert(id)
      // debugger
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用
          return dectApi.removeById(id);
        })
        .then((res) => {
          //刷新页面
          this.getDataList();
          //提示信息
          this.$message.success(response.message || "删除成功");
        });
    },
  },
  mounted(){
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
